package com.atom.process.cnmuseum.bm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.task.Task;
import org.springframework.stereotype.Service;

import com.atom.common.VariablesConstant;
import com.atom.common.exception.AvensNullPointerException;
import com.atom.common.remote.user.UserUtil;
import com.atom.common.util.PreConditionUtil;
import com.atom.process.cnmuseum.bm.entity.task.BmTaskDO;
import com.atom.process.cnmuseum.bm.tool.ProcessDefinitions;
import com.atom.process.cnmuseum.bm.tool.TaskDefinitions;
import com.atom.process.cnmuseum.bm.vo.request.GcxxRequestVO;
import com.atom.process.cnmuseum.bm.vo.request.RequestVO;
import com.atom.process.cnmuseum.bm.vo.request.WhrlxxxRequestVO;
import com.atom.process.cnmuseum.bm.vo.request.XzxxRequestVO;
import com.atom.process.cnmuseum.bm.vo.request.YxxxRequestVO;
import com.sinosoft.cnmuseum.utils.AuthorityUtil;
import com.sinosoft.cnmuseum.utils.LogUtils;

@Service
public class BianmuService extends AbstractService implements ServiceInterface {

	public synchronized String create(UserUtil user, String gcxxid) {
		Map<String, Object> var_start = new HashMap<String, Object>();
		// 指定起草人
		var_start.put("nextAssignee", user.getUserid());
		// 启动流程
		String processInstanceId = activitiHandler.startProcessInstanceByKey(ProcessDefinitions.PROCESS_DEF_KEY, var_start);
		// 生成标题
		String biaoTi = "编目_" + gcxxid;
		// 初始化流程变量
		activitiHandler.initProcessVariables(ProcessDefinitions.PROCESS_DEF_KEY, processInstanceId, user,biaoTi, null);
		/* 初始化流程数据 */
		// 创建PDO
		createPdo(user,processInstanceId,biaoTi,gcxxid);
		
		updateGcxxBmbs(gcxxid,"1");
		// 获取申请任务
		Task task = activitiHandler.findTaskByProcessIdByTaskDefinitionKey(processInstanceId, TaskDefinitions.编目.getKey());
		if (task != null) {
			String taskId = task.getId();
			// 创建ADO
			createAdo(user, processInstanceId, taskId, biaoTi,gcxxid);
			
			// 创建流程
			activitiHandler.createRemoteRunning(taskId, processInstanceId, user);
			return taskId;
		} else {
			throw new AvensNullPointerException("task is null where processInstanceId = "
		                                         + processInstanceId + " and taskDefinitionKey = "
					                             + TaskDefinitions.编目.getKey());
		}
	}

	
	@Override
	public void save(RequestVO vo,GcxxRequestVO gcxxvo, XzxxRequestVO xzxxvo, YxxxRequestVO yxxxvo, WhrlxxxRequestVO whrlxxxvo, UserUtil user) {
		/* 参数校验 */
		paramsCheck(vo);

		String taskId = vo.getTaskId();
		String processInstanceId = vo.getProcessId();
		/* 处理流程数据 */
    	String onetitle = "编目" + gcxxvo.getCollection_code();
		// 更新ADO
		saveAdo(taskId, vo,gcxxvo,xzxxvo, yxxxvo,whrlxxxvo,user,onetitle);
		// 创建流程
		activitiHandler.createRemoteRunning(taskId, processInstanceId, user);
		// 更新流程变量
		// 生成标题
		String title = "编目_" + gcxxvo.getCollection_code();
		activitiHandler.updateProcessInstanceName(taskId, processInstanceId, title);
	}

	@Override
	public void submit(RequestVO vo, UserUtil user) {
		/* 参数校验 */
		paramsCheck(vo);
		String taskId = vo.getTaskId();
		String mainProcessInstanceId = vo.getProcessId();
		// 完成任务
		Map<String, Object> var_complete = new HashMap<String, Object>();
		var_complete.put(VariablesConstant.PREVIOUS_ASSIGNEE, user.getUserid());
		// 提交录入或审核
		String next = vo.getNext();
		if (next.equals("lr")) {
			String nextids = vo.getNextids();
			var_complete.put("taskFlowType", next);
			var_complete.put("nextAssignee", nextids);
		} else if(next.equals("sh")){//内容审核
			String nextids = AuthorityUtil.getUseridsByJsid(AuthorityUtil.jsid_bm_nrshr);
			
			var_complete.put("taskFlowType", next);
			var_complete.put("nextAssignee", nextids);
		}

		/* 处理流程数据 */
		// 更新流程变量
		// 生成标题
		String title = vo.getBiaoti();
		activitiHandler.updateProcessInstanceName(taskId, mainProcessInstanceId, title);
		
		// 更新PDO
		ADO2PDO(mainProcessInstanceId, vo.getTaskId());
		commentDao.saveComment(vo.getYourAdvice(), user, null, 0, taskId, mainProcessInstanceId, TaskDefinitions.编目.getKey());
		/* 处理流程数据 */
		// 完成任务/写入已办表/删除待办表
		activitiHandler.completeTask(taskId, var_complete);
		
        //生成日志
        LogUtils logUtils = new LogUtils();
        String desc = title+LogUtils.MbXtglSJBJ_SUBMIT_DESC;
        logUtils.addLog(String.valueOf(LogUtils.type_submit), LogUtils.model_sjbj, user.getUserid(), desc);
	}

	public void delete(RequestVO vo, UserUtil user) {
		/* 参数校验 */
		paramsCheck(vo);
		String taskId = vo.getTaskId();
		String processInstanceId = vo.getProcessId();
		
		List<BmTaskDO> bmlist = bmTaskDao.findByProperty("taskId", taskId);
		if(bmlist!=null&&bmlist.size()>0){
			BmTaskDO bm = bmlist.get(0);
			updateGcxxBmbs(bm.getGcxxid(),"0");
		}
		
		activitiHandler.deleteProcess(processInstanceId, taskId);
        //生成日志
        LogUtils logUtils = new LogUtils();
        String desc = vo.getBiaoti()+LogUtils.MbXtglSJBJ_DELETE_DESC;
        logUtils.addLog(String.valueOf(LogUtils.type_delete), LogUtils.model_sjbj, user.getUserid(), desc);

	}

	private void paramsCheck(RequestVO vo) {
		PreConditionUtil.checkNotNull(vo.getTaskId(), "taskId is null");
	}

}
